//
//  TimingSettingModel.m
//  wAudioShare
//
//  Created by Anson on 15-2-9.
//  Copyright (c) 2015年 赵帅. All rights reserved.
//

#import "TimingSettingModel.h"

@implementation TimingSettingModel

- (instancetype)initWithFixedDateTime:(NSDate *)dateTime {

    self = [super init];
    if (self) {
        _runType = TimingSettingRunTypeFixedDay;
        _date = dateTime;
    }
    return self;
}

- (instancetype)initWithWeek:(NSString *)weeks andTime:(NSDate *)time {

    self = [super init];
    if (self) {
        _runType = TimingSettingRunTypeWeek;
        _weeks = weeks;
        _date = time;
    }
    return self;
}

- (SceneDeviceModel *)deviceModel {
    if (!_deviceModel) {
        _deviceModel = [[SceneDeviceModel alloc] init];
    }
    return _deviceModel;
}


+ (NSString *)getDescriptForWeeksVal:(NSArray *)weeks {
    
    NSComparator cmptr = ^(id obj1, id obj2){
        if ([obj1 integerValue] > [obj2 integerValue]) {
            return (NSComparisonResult)NSOrderedDescending;
        }
        
        if ([obj1 integerValue] < [obj2 integerValue]) {
            return (NSComparisonResult)NSOrderedAscending;
        }
        return (NSComparisonResult)NSOrderedSame;
    };
    
    NSMutableString *ret = [[NSMutableString alloc] initWithString:@""];
    NSArray *sortArray = [weeks sortedArrayUsingComparator:cmptr];
    for (NSString *item in sortArray) {
        
        switch ([item integerValue]) {
            case 0:
                [ret appendString:@"周日 "];
                break;
            case 1:
                [ret appendString:@"周一 "];
                break;
            case 2:
                [ret appendString:@"周二 "];
                break;
            case 3:
                [ret appendString:@"周三 "];
                break;
            case 4:
                [ret appendString:@"周四 "];
                break;
            case 5:
                [ret appendString:@"周五 "];
                break;
            case 6:
                [ret appendString:@"周六 "];
                break;
            default:
                break;
        }
    }
    return ret;
}

+ (NSString *)getDateTimeVal:(NSDate *)date {
    
    if (date) {
        
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"YYYYMMdd"];
        NSString *str = [formatter stringFromDate:date];
        
        [formatter setDateFormat:@"HHmmss"];
        NSString *str2 = [formatter stringFromDate:date];
        return [str stringByAppendingFormat:@"T%@", str2];
    }
    return @"";
}
//!** ISO格式 20141127T153000 截取153000 **/
+ (NSString *)getTimeFromISOString:(NSString *)str {
    
    NSArray *list = [str componentsSeparatedByString:@"T"];
    if (list.count == 2) {
        NSString *time = list.lastObject;
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"HHmmss"];
        NSDate *date = [formatter dateFromString:time];
        if (date) {
            [formatter setDateFormat:@"HH:mm:ss"];
            return [formatter stringFromDate:date];
        }
    }
    return @"";
}

//!** ISO格式 20141127T153000 截取20141127 **/
+ (NSString *)getDateFromISOString:(NSString *)str {
    
    NSArray *list = [str componentsSeparatedByString:@"T"];
    if (list.count == 2) {
        NSString *dateStr = list.firstObject;
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"YYYYMMdd"];
        NSDate *date = [formatter dateFromString:dateStr];
        if (date) {
            [formatter setDateFormat:@"YYYY-MM-dd"];
            return [formatter stringFromDate:date];
        }
    }
    return @"";
    
}

@end
